package cc.rengu.redp.flowable.listener;

import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 全局监听器配置
 */
@Configuration
public class GlobalEventListenerConfig {

    @Autowired
    private GlobalEventListener globalEventListener;

    @Bean
    public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> globalListenerConfigurer() {
        return engineConfiguration -> {
            engineConfiguration.setTypedEventListeners(this.customFlowableListeners());
        };
    }

    private Map<String, List<FlowableEventListener>> customFlowableListeners() {
        Map<String, List<FlowableEventListener>> listenerMap = new HashMap<>();
        List<FlowableEventListener> flowableEventListeners = new ArrayList<>();
        flowableEventListeners.add(globalEventListener);
        listenerMap.put("TASK_CREATED", flowableEventListeners);
        listenerMap.put("TASK_COMPLETED", flowableEventListeners);
        listenerMap.put("PROCESS_STARTED", flowableEventListeners);
        listenerMap.put("PROCESS_COMPLETED", flowableEventListeners);
        return listenerMap;
    }
}
